using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Commons.Models.Settings.Matchers.Operators.Relational
{
	[Serializable]
	public class EqualOperator<T> : IRelationalOperator<T>
	{
		public virtual OperatorType Type
		{
			get
			{
				return OperatorType.Eq;
			}
		}

		public virtual bool Operate(T v1, T v2)
		{
			if (v1 == null)
			{
				return false;
			}
			return AreIntersected(GetSet(v1), GetSet(v2));
		}

		private bool AreIntersected(IEnumerable<string> col1, IEnumerable<string> col2)
		{
			return col1.Intersect(col2).Count() > 0;
		}

		private IEnumerable<String> GetSet(T v)
		{
			if (v is IEnumerable<String>)
			{
				return (IEnumerable<String>) v;
			}
			else
			{
				HashSet<string> col = new HashSet<string>();
				if (!(v is string) && v is IEnumerable)
				{
					foreach (object o in (IEnumerable) v)
					{
						col.Add(RelationalOperatorSupport.GetString(o));
					}
				}
				else
				{
					col.Add(RelationalOperatorSupport.GetString(v));
				}
				return col;
			}
		}
	}
}
